package fr.afcepf.al22.qualite.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;




import com.mysql.jdbc.Statement;

import fr.afcepf.al22.qualite.entitee.Personne;
/**
 * Fhddkjdjkbd.
 * @author Stagiaire
 *
 */
public class DaoPersonneImpl implements IDaoPersonne {
	/**
	 * Gjhdjhd.
	 */
	private DataSource ds = new Al22DataSource();
	/**
	 *
	 */
	private final String reqAjout = "INSERT INTO personne"
			+ "(nom, mail, mdp, naissance)";
	/**
	 *
	 */
	private final int indiceNom = 1;
	/**
	 *
	 */
	private final int indiceMail = 2;
	/**
	 *
	 */
	private final int indiceMdp = 3;
	/**
	 *
	 */
	private final int indiceNaissance = 4;
	@Override
	public Personne ajouter(Personne pers) {
		Connection cnx = null;
		try {
			cnx = ds.getConnection();
			PreparedStatement pstmt = cnx.prepareStatement("",
				Statement.RETURN_GENERATED_KEYS);
			pstmt.setString(indiceNom, pers.getNom());
			pstmt.setString(indiceMail, pers.getMail());
			pstmt.setString(indiceMdp, pers.getMdp());
			pstmt.setDate(indiceNaissance, new
					java.sql.Date(pers.getNaissance().
							getTime()));
			
			int nbLigne = pstmt.executeUpdate();
			ResultSet rs = pstmt.getGeneratedKeys();
			while (nbLigne == 1 && rs.next()) {
				pers.setId(rs.getInt(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				cnx.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return pers;
	}

	@Override
	public List<Personne> rechercher(String nom) {
		return null;
	}

	@Override
	public Personne verifMail(String mail) {
		return null;
	}

}
